// 文章相关的路由
const express = require('express');
const db = require('../db');
const router = express.Router();

// ======================= 获取文章列表接口 =======================
router.get('/list', async(req,res) => {
    let {pagenum, pagesize, state, cate_id} = req.query;
    // 判断是否有必填参数
    if(pagenum === undefined || pagesize === undefined) {
        res.send({status: 0, message: '缺少必填参数'});
        return
    }
    // 判断客户端是否使用了 cate_id  state 条件
    let w = '';
    if(state) {
        w += ` and state='${state}' `;
    }
    if(cate_id) {
        w += ` and cate_id=${cate_id} `;
    }
    let sql = `select a.Id, title, state, pub_date, name cate_name from article a join 
    category c on a.cate_id=c.Id
    where is_delete=0 and author_id=? ${w}
    limit ${(pagenum - 1) * pagesize}, ${pagesize}`;
    let r = await db(sql,req.user.id);
    // 查询总记录数
    let sql2 = `select count(*) total from article a join 
    category c on a.cate_id=c.Id
    where is_delete=0 and author_id=? ${w}`;
    let r2 = await db(sql2,req.user.id);
    if(r) {
        res.send({status: 0, message: '获取文章列表成功', data: r, total: r2[0].total})
    }else {
        res.send({status: 1, message: '获取文章列表失败'})
    }
});

// ========================= 根据文章 Id 删除一篇文章的接口 =====================================
router.get('/delete/:id', async (req, res) => {
    let id = req.params.id;
    let r = await db('update article set is_delete=1 where Id=?', id);
    if (r && r.affectedRows > 0) {
        res.send({ status: 0, message: '删除文章成功' });
    } else {
        res.send({ status: 1, message: '删除文章失败' })
    }
});

// ============================ 根据文章 Id 获取一篇文章的接口 ==================================
router.get('/:id', async (req, res) => {
    let id = req.params.id;
    // 根据id 去查询一篇文章
    let r = await db('select * from article where Id=?', id)
    if (r && r.length > 0) {
        res.send({ status: 0, message: '获取一篇文章成功', data: r[0] });
    } else {
        res.send({ status: 1, message: '获取一篇文章失败' })
    }
});

// ============================ 添加文章 ==================================
const multer = require('multer');
const moment = require('moment');
const upload = multer({ dest: 'uploads/' });
router.post('/add', upload.single('cover_img'), async (req, res) => {
    // req.file -- 获取上传文件信息
    // req.body -- 获取提交的文本信息
    let obj = req.body;
    obj.cover_img = req.file.filename; // 添加图片的文字
    obj.pub_date = moment().format('YYYY-MM-DD HH:mm:ss'); // 添加发布时间
    obj.author_id = req.user.id; // 添加作者的id
    let r = await db('insert into article set ?', obj);
    if (r && r.affectedRows > 0) {
        res.send({ status: 0, message: '添加文章成功' });
    } else {
        res.send({ status: 1, message: '添加文章失败' })
    }
});

// ============================ 根据文章 Id 修改一篇文章的接口 ==================================
router.post('/edit', upload.single('cover_img'), async (req, res) => {
    // req.file -- 获取上传文件信息
    // req.body -- 获取提交的文本信息
    let obj = req.body;
    obj.cover_img = req.file.filename; // 添加图片的文字
    // obj.pub_date = moment().format('YYYY-MM-DD HH:mm:ss'); // 添加发布时间
    // obj.author_id = req.user.id; // 添加作者的id
    let r = await db('update article set ? where Id=?', [obj,req.body.Id]);
    if (r && r.affectedRows > 0) {
        res.send({ status: 0, message: '修改文章成功' });
    } else {
        res.send({ status: 1, message: '修改文章失败' })
    }
})

module.exports = router;